//
// Created by jsjxsy on 2022/8/17.
//
#include <iostream>
#include <cstdio>
using namespace std;
class Base {
 public:
  virtual ~Base() {
    cout << "~Base()" << endl;
  }
  virtual void say() {
    cout << "this is base class" << endl;
  }
};

class Derived : public Base {
 public:
  void say() {
    cout << "this is Derived class" << endl;
  }
  ~Derived() {
    cout << "~Derived()" << endl;
  }

};
void cast_to_ref(const Base &b);

int main() {
  Base *bp;
  bp = new Derived();
  cast_to_ref(*bp);
//  bp = new Base();
//  cast_to_ref(*bp);
  return 0;
}
void cast_to_ref(const Base &b) {
  try {
    Derived d = dynamic_cast<const Derived &>(b);
    d.say();
  } catch (bad_cast &e) {
    cout << "Base class cast Derived class error" << endl;
  } catch (exception &e) {
    cout << "unknown exception" << endl;
  }
}